#region license
// KenEgozi.com - my blog engine
// This project is licensed under the MIT License
//
//Copyright (c) 2007, Ken Egozi
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion

using System.Collections.Generic;
using Castle.Components.DictionaryAdapter;
using Castle.MonoRail.Framework;
using System.Web;
using KenEgozi.Com.Domain.Aggregates.Post;
using KenEgozi.Com.Domain.Repositories;
using KenEgozi.Com.Weblog.Filters;
using KenEgozi.Com.Weblog.Interfaces;
using KenEgozi.Com.Weblog.Services;
using NHibernate.Expression;
using Rhino.Commons;

namespace KenEgozi.Com.Weblog.Controllers.Admin
{
	/// <summary>
	/// Adding comments to a post
	/// </summary>
	[ControllerDetails(Area = "Admin")]
	[Filter(ExecuteEnum.BeforeAction, typeof(AdminOnlyFilter))]
	public class CommentsController : AbstractController
	{
		readonly IRepository<Comment> commentRepository;
		public CommentsController(ICachedItemsService CachedItemsService, IDictionaryAdapterFactory dictionaryAdapterFactory, IRepository<Comment> commentRepository)
			: base(CachedItemsService, dictionaryAdapterFactory)
		{
			this.commentRepository = commentRepository;
		}

		[Cache(HttpCacheability.NoCache)]
		public void New(int postId)
		{
			CancelLayout();
			RenderView("commentsList");
			ICommentsView commentsView = DictionaryAdapterFactory.GetAdapter<ICommentsView>(PropertyBag);
			commentsView.Comments = Repository.Posts.Get(postId).NewComments;
		}

		public void NewByPosts()
		{
			SetLayoutParameters();
			IPostsView postsView = DictionaryAdapterFactory.GetAdapter<IPostsView>(PropertyBag);
			DetachedCriteria crit = DetachedCriteria.For<Post>()
				.CreateAlias("Comments", "c")
				.Add(Expression.Eq("c.Status", CommentStatus.ProbableOk));
			ICollection <Post> posts = Repository.Posts.FindAll(crit);
			IDictionary<Post, Post> uniquePosts = new Dictionary<Post, Post>();
			foreach (Post post in posts)
			{
				uniquePosts[post] = post;
			}
			posts.Clear();
			foreach (KeyValuePair<Post, Post> pair in uniquePosts)
			{
				posts.Add(pair.Value);
			}
			postsView.Posts = posts;
		}

		public void Approve(int id)
		{
			CancelLayout();
			try
			{
				Comment comment = commentRepository.Get(id);
				comment.Approve();
				With.Transaction(delegate { commentRepository.Update(comment); });
				RenderText("OK");
			}
			catch
			{
				RenderText("PROBLEM");
			}
		}
		public void Delete(int id)
		{
			CancelLayout();
			try
			{
				Comment comment = commentRepository.Get(id);
				With.Transaction(delegate { commentRepository.Delete(comment); });
				RenderText("OK");
			}
			catch
			{
				RenderText("PROBLEM");
			}
		}
	}
}
